Replace minimist with dashdash for CLI argument parsing#614
Closed
KAMRONBEK wants to merge 1 commit into
Closed
Conversation
minimist is deprecated and has a history of parsing bugs. Port the argument parsing in src/index.ts to dashdash (already a transitive dependency), keeping runtime behavior identical for every flag and for positional package names. dashdash converts dashes in option names to underscores in the parsed result (--patch-dir -> opts.patch_dir) and puts positionals in opts._args; 'help'/'h' and 'version'/'v' are declared as aliases. Absent options are omitted from the result, so the existing "rebase"/"append" in argv presence checks are unchanged. Unknown options are now rejected with a clear message and exit 1 instead of being silently swallowed; the parse call is wrapped so errors print cleanly. Removes minimist and @types/minimist as direct deps (obviating the CVE bump in ds300#521 for the direct dependency edge) and adds dashdash + @types/dashdash. Verified: yarn build (zero errors), tslint clean, and the full test suite (./run-tests.sh --runInBand) passes 47 suites / 660 tests / 108 snapshots.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #428.
What & why
minimistis deprecated and has a history of parsing bugs (see the issuesreferenced in #428, plus #412 and the
#250thread). This PR portssrc/index.tsfromminimisttodashdash,a strict, spec-driven option parser (already a transitive dependency of this
project via other packages, so it adds no new install footprint).
Runtime behavior is kept identical for every documented flag and for
positional package-name arguments. The one intentional improvement is that
dashdashrejects unknown options with a clear message and exit code 1,whereas
minimistsilently swallowed them (and could even eat the followingpositional as the unknown flag's value — one of the "argument parser is broken"
symptoms #428 is about).
Relationship to #521 (minimist CVE bump)
#521 bumps
minimist^1.2.6 → ^1.2.8(and@types/minimist^1.2.2 → ^1.2.5)to remediate CVE-2021-44906 (prototype pollution).
This PR obviates #521 for patch-package's own dependency: it removes
minimistand@types/minimistas direct dependencies entirely, so thevulnerable direct edge #521 patches no longer exists.
minimiststill appearsin
yarn.lockonly as a transitive dependency of other packages(
minimist@^1.1.1, minimist@^1.2.0— pulled in by e.g.mkdirp), which isoutside both this PR's and #521's scope. If maintainers prefer the minimal
CVE-only change, #521 is the safer pick; if they want the deprecated parser gone,
this PR supersedes it.
Flag-by-flag mapping
dashdashdiffers fromminimistin two mechanical ways that the port accountsfor: (1) option names have their dashes converted to underscores in the parsed
result (
--patch-dir→opts.patch_dir), and (2) positional arguments live inopts._args(notopts._). Absent options are omitted from the result object,so the existing
"rebase" in argv/"append" in argvpresence checks keepworking unchanged.
--help,-hargv.help || argv.hargv.helpnames: ["help","h"];-hnow setshelpdirectly--version,-vargv.version || argv.vargv.versionnames: ["version","v"]--use-yarnargv["use-yarn"]argv.use_yarn--case-sensitive-path-filteringargv["case-sensitive-path-filtering"]argv.case_sensitive_path_filtering--reverseargv["reverse"]argv.reverse--error-on-failargv["error-on-fail"]argv.error_on_fail--error-on-warnargv["error-on-warn"]argv.error_on_warn--create-issueargv["create-issue"]argv.create_issue--partialargv.partialargv.partial--patch-dir <dir>argv["patch-dir"]argv.patch_dir--append [<name>]argv.append,"append" in argvargv.append,"append" in argv--rebase <patch>argv.rebase,"rebase" in argvargv.rebase,"rebase" in argv--include <regexp>argv.includeargv.include--exclude <regexp>argv.excludeargv.excludeargv._argv._argsDeliberate decisions / behavioral notes
--include/--excludearetype: "string"(single value), notarrayOfString. The downstream consumermakeRegExp(reString: string, …)takes a string. Under
minimist, repeating--include a --include bproduced an array
["a","b"]that was then coerced to the string"a,b"andfed to
new RegExp()— effectively undefined/broken behavior.dashdashwithtype: "string"gives last-value-wins ("b"), which is the sane single-valuecontract the code already assumes. No integration test passes the flag more
than once.
silently absorbed. This is the desired fix, not a regression.
chalk.red(...)followed byprocess.exit(1), so users get a clean one-line message rather than a stacktrace.
--append/--rebase(no value) will now be a parse error rather thanfalling through to the graceful "you must specify…" message. This path is not
reachable in any integration test (all usages supply a value, e.g.
--rebase 0,--append 'WhileOne'), and requiring a value for avalue-bearing flag is correct behavior.
Dependency changes (
package.json)minimist(dependencies) and@types/minimist(devDependencies).dashdash@^1.14.1(dependencies) and@types/dashdash@^1.14.3(devDependencies).
dashdashships no bundled types, so the DefinitelyTypedpackage is used.
Verification (all run locally)
yarn build(tsc --project tsconfig.build.json) — passes, zero errors.tslint --project tsconfig.json src/index.ts— clean../run-tests.sh --runInBand(clean build + pack tarball + full Jest suite,incl. all integration + property-based tests) —
47 suites passed, 660 tests passed, 224 skipped, 108 snapshots passed, 0 failed.
Notable flag-exercising suites that passed:
rebase-zero(--rebase/--append),include-exclude-regex-relativity&include-exclude-paths(
--include/--exclude),error-on-warn,error-on-fail,partial-apply(
--partial),reverse-option/reverse-multiple-patches(--reverse),custom-patch-dir(--patch-dir),create-issue(--create-issue),append-patches,happy-path-npm,happy-path-yarn.dist/index.js:--version/-v→ printspatch-package <version>and exits (noop). ✔--help→ prints full usage. ✔patch-package,--patch-dir mypatches,--reverse) →Applying patches… / No patch files found. ✔--frobnicate somepkg(unknown flag) →unknown option: "--frobnicate",exit code 1. ✔
positionals, rebase/append presence, include/exclude, all booleans) across
16 representative arg combinations showed identical results between the old
minimistconfig and the newdashdashparser.